package com.akk.platform.oem.business;

import com.akk.module.avs.entity.AvsYbTagType;
import com.akk.module.avs.entity.AvsYbTags;
import com.akk.module.avs.model.response.AvsYbCardTagsRes;
import com.akk.module.avs.service.AvsYbTagTypeService;
import com.akk.module.avs.service.AvsYbTagsService;
import com.akk.utils.PageUtils;
import com.akk.utils.Query;
import com.akk.utils.RestResponse;
import com.akk.utils.ShiroUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class YbCardTagTypeBusiness {

    @Autowired
    private AvsYbTagTypeService tagTypeService;

    @Autowired
    private AvsYbTagsService tagsService;

    public RestResponse allTagTypes(){
        Map params = new HashMap();
        params.put("appId",ShiroUtils.getAdmin().getAppId());
        List list = tagTypeService.findAllByParams(params);
        return RestResponse.build(list);
    }

    public RestResponse list(Query query,BindingResult bindingResult){
        if (bindingResult.hasErrors())
            return RestResponse.build("1",bindingResult.getFieldError().getDefaultMessage(),null);
        Map params = query.getSearch();
        params.put("appId", ShiroUtils.getAdmin().getAppId());
        PageHelper.startPage(query.getPage(),query.getLimit());
        List<AvsYbTagType> list = tagTypeService.findAllByParams(params);
        return RestResponse.build(PageUtils.buildPage(list,(int) new PageInfo<>(list).getTotal(),query));
    }

    public RestResponse save(AvsYbTagType avsYbTagType, BindingResult bindingResult){
        if (bindingResult.hasErrors())
            return RestResponse.build("1",bindingResult.getFieldError().getDefaultMessage(),null);
        avsYbTagType.setAppId(ShiroUtils.getAdmin().getAppId());
        tagTypeService.save(avsYbTagType);
        return RestResponse.build("200","success",null);
    }

    public RestResponse del(Long tagTypeId){
        tagTypeService.del(tagTypeId);
        return RestResponse.build("200","success",null);
    }

    public RestResponse tagsList(Query query,BindingResult bindingResult){
        if (bindingResult.hasErrors())
            return RestResponse.build("1",bindingResult.getFieldError().getDefaultMessage(),null);
        Map params = query.getSearch();
        PageHelper.startPage(query.getPage(),query.getLimit());
//        List<AvsYbTags> list = tagsService.findByParams(params);
        List<AvsYbCardTagsRes> list = tagsService.find(params);
        return RestResponse.build(PageUtils.buildPage(list,(int) new PageInfo<>(list).getTotal(),query));
    }

    public RestResponse tagsSave(AvsYbTags tags,BindingResult bindingResult){
        if (bindingResult.hasErrors())
            return RestResponse.build("1",bindingResult.getFieldError().getDefaultMessage(),null);
        tags.setAppId(ShiroUtils.getAdmin().getAppId());
        tagsService.save(tags);
        return RestResponse.build("200","success",null);
    }

    public RestResponse tagsDel(Long tagId){
        tagsService.del(tagId);
        return RestResponse.build("200","success",null);
    }

    public RestResponse allTags(){
        Map params = new HashMap();
        params.put("appId",ShiroUtils.getAdmin().getAppId());
        List<AvsYbTags> list = tagsService.findByParams(params);
        return RestResponse.build(list);
    }

    public RestResponse cardTags(Long cardId){
        return RestResponse.build(tagsService.cardTags(cardId));
    }

}
